package com.example.samplehelloandroid3;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener{

	EditText inputTextView;
	TextView showTextView;
	int totalNum = 0;
	String saveKey = "";
	String partStr = "";
	String stateKey = "";
	
	ArrayList<Integer> numLst = new ArrayList<Integer>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inputTextView = (EditText)findViewById(R.id.inputText);
		showTextView = (TextView)findViewById(R.id.textView1);
		Button btn0 = (Button)findViewById(R.id.button0);
		Button btn1 = (Button)findViewById(R.id.button1);
		Button btn2 = (Button)findViewById(R.id.button2);
		Button btn3 = (Button)findViewById(R.id.button3);
		Button btn4 = (Button)findViewById(R.id.button4);
		Button btn5 = (Button)findViewById(R.id.button5);
		Button btn6 = (Button)findViewById(R.id.button6);
		Button btn7 = (Button)findViewById(R.id.button7);
		Button btn8 = (Button)findViewById(R.id.button8);
		Button btn9 = (Button)findViewById(R.id.button9);
		Button btn11 = (Button)findViewById(R.id.button11);
		Button btn12 = (Button)findViewById(R.id.button12);
		
		btn0.setOnClickListener(this);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
		btn7.setOnClickListener(this);
		btn8.setOnClickListener(this);
		btn9.setOnClickListener(this);
		btn11.setOnClickListener(this);
		btn12.setOnClickListener(this);
		
		stateKey = "left";
	}
	
	public void onClick(View v) {
		//showTextView.setText(inputTextView.getText().toString());
		int num = 0;
		char sign = 'a';
		
		switch(v.getId()){
		case R.id.button1:
			num = 1;
			break;
	
		case R.id.button2:
			num = 2;
			break;
		case R.id.button3:
			num = 3;
			break;
		case R.id.button4:
			num = 4;
			break;
		case R.id.button5:
			num = 5;
			break;
		case R.id.button6:
			num = 6;
			break;
		case R.id.button7:
			num = 7;
			break;
		case R.id.button8:
			num = 8;
			break;
		case R.id.button9:
			num = 9;
			break;
		case R.id.button0:
			num = 0;
			break;
		case R.id.button11:
			sign = '+';
			break;
		case R.id.button12:
			sign = '=';
			break;
		}
		
		String textStr = "";
		
		if(v.getId() == R.id.button11 || v.getId() == R.id.button12){
			if(v.getId() == R.id.button11){
				numLst.add(Integer.parseInt(partStr));
				partStr = "";
				saveKey = saveKey + sign;
				
			}else if(v.getId() == R.id.button12){
				numLst.add(Integer.parseInt(partStr));
				for(int lstNum = 0; lstNum < numLst.size(); lstNum++){
					totalNum += numLst.get(lstNum);
				}
				
				saveKey = saveKey + sign + String.valueOf(totalNum);
//				numLst = new ArrayList<Integer>();
//				numLst.add(totalNum);
			}
		}else{
			saveKey = saveKey + String.valueOf(num);
			partStr = partStr + String.valueOf(num);
			
			System.out.println("partStr = >" + partStr);
		}
		
		inputTextView.setText(saveKey);
	}

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	};

}
